package com.zhejiangzhengyuan.municipal_common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author lin_ju
 * @create on 2019/11/26 拦截器配置
 */
@Configuration
public class ApplicationConfig implements WebMvcConfigurer {
    @Autowired
    private JwtFilter jwtFilter;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //功能以测通 接口开发阶段  暂时屏蔽
        registry
                .addInterceptor(jwtFilter)
                .excludePathPatterns("/**/**/login")
                .excludePathPatterns("/doc.html")
                .excludePathPatterns("/swagger-resourcnes")
                .excludePathPatterns("/security/subject/logi");
//                .excludePathPatterns("/**");

    }

    /**
     * 添加路径映射,将真实路径映射为虚拟路径
     * 读取静态文件
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        String os = System.getProperty("os.name");
        if (os.toLowerCase().contains("window")) {
            //若当前系统是window系统
            registry.addResourceHandler("/uploads/**").addResourceLocations("file:c:/municipal/");
        }else {
            registry.addResourceHandler("/uploads/**").addResourceLocations("file:/home/municipal/");
        }
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }

}
